package com.fanxuankai.boot.canal.util;

import cn.hutool.core.text.StrPool;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.fanxuankai.boot.canal.client.autoconfigure.CanalClientProperties;
import com.fanxuankai.boot.canal.client.EntryWrapper;
import org.springframework.util.StringUtils;

/**
 * 主题
 *
 * @author fanxuankai
 */
public class Topic {

    protected static final String CANAL_2_MQ = "canal2Mq";

    public static String of(String schema, String table) {
        return CANAL_2_MQ + StrPool.UNDERLINE + schema + StrPool.UNDERLINE + table;
    }

    public static String of(String schema, String table, String eventType) {
        return CANAL_2_MQ + StrPool.UNDERLINE + schema + StrPool.UNDERLINE + table + StrPool.UNDERLINE + eventType;
    }

    public static String custom(String queue, String eventType) {
        return CANAL_2_MQ + StrPool.UNDERLINE + queue + StrPool.UNDERLINE + eventType;
    }

    public static String custom(String queue) {
        return CANAL_2_MQ + StrPool.UNDERLINE + queue;
    }

    public static String customWithoutPrefix(String queue, String eventType) {
        return queue + StrPool.UNDERLINE + eventType;
    }

    public static String of(EntryWrapper entryWrapper, CanalClientProperties properties) {
        String topic = properties.getTopic(entryWrapper);
        CanalEntry.EventType eventType = entryWrapper.getEventType();
        if (StringUtils.hasText(topic)) {
            return Topic.custom(topic, eventType.name());
        }
        return Topic.of(entryWrapper.getSchemaName(), entryWrapper.getTableName(), eventType.name());
    }
}
